package com.punai.monthanalysis.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.multi.ListValueMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.BaseEntity;
import com.punai.externalccb.domain.YdCbfeeiteminfoStandard;
import com.punai.monthanalysis.bo.ProYdcbfxClzlkzqkbBo;
import com.punai.monthanalysis.domain.ProYdcbfxClzlkzqkb;
import com.punai.monthanalysis.mapper.ProYdcbfxClzlkzqkbMapper;
import com.punai.monthanalysis.service.IProYdcbfxClzlkzqkbService;
import com.punai.monthanalysis.sqliteDao.CostStructureCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 材料总量控制情况Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-19
 */
@Service
public class ProYdcbfxClzlkzqkbServiceImpl extends ServiceImpl<ProYdcbfxClzlkzqkbMapper,ProYdcbfxClzlkzqkb> implements IProYdcbfxClzlkzqkbService
{
    @Autowired
    private ProYdcbfxClzlkzqkbMapper proYdcbfxClzlkzqkbMapper;
    @Autowired
    private CostStructureCategoryRepository costStructureCategoryRepository;


    @Override
    public List<? extends BaseEntity> getTabsDataList(String proId,String monthId,List<YdCbfeeiteminfoStandard> list,List<YdCbfeeiteminfoStandard> categorysList) {
        ListValueMap<String,YdCbfeeiteminfoStandard> listValueMap = new ListValueMap<>();
        list.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        categorysList.forEach(ydCbfeeiteminfoStandard -> {
            listValueMap.putValue(ydCbfeeiteminfoStandard.getOpid()+"",ydCbfeeiteminfoStandard);
        });
        List<YdCbfeeiteminfoStandard> roots = listValueMap.get("0");
        List<ProYdcbfxClzlkzqkbBo> result = new ArrayList<>();

        if (CollUtil.isNotEmpty(roots)){
            for (YdCbfeeiteminfoStandard root : roots) {
                ProYdcbfxClzlkzqkbBo convert = convert(root);
                result.add(convert);
                setChildren(convert,listValueMap);
            }
        }
        return result;
    }
    private void setChildren(ProYdcbfxClzlkzqkbBo parent,
                             ListValueMap<String, YdCbfeeiteminfoStandard> listValueMap) {

        String id = parent.getId();
        List<YdCbfeeiteminfoStandard> children = listValueMap.get(id);
        if (CollUtil.isNotEmpty(children)) {
            for (YdCbfeeiteminfoStandard child : children) {
                ProYdcbfxClzlkzqkbBo convert = convert(child);
                parent.getChildren().add(convert);
                setChildren(convert,listValueMap);
            }
        }
    }

    private ProYdcbfxClzlkzqkbBo convert(YdCbfeeiteminfoStandard in) {
        ProYdcbfxClzlkzqkbBo out = new ProYdcbfxClzlkzqkbBo();
        out.setId(in.getOid()+"");
        out.setProId(in.getProId());
        out.setQc(in.getYdDate());//期次Id
        out.setItemName(in.getName());
//        out.setName(in.getName());
        out.setUnit(in.getUnit());

        out.setSlkzzlmbcb(in.getTargetAmount());
        out.setPrice(in.getTargetNoTaxPrice());
        out.setTotal(in.getTargetNoTaxTotal());

        out.setCurrmonthxhsl(in.getActualCostAmout());
        out.setCurrmonthxhje(in.getActualCostNoTaxTotal());
        out.setKlmonthxhsl(in.getKlActualAmout());
        out.setKlmonthxhje(in.getKlActualCostNoTaxTotal());

        out.setKljcsl(in.getDifferenceOfCzsjAmount());
        out.setKljcje(in.getKlDifferenceOfCzsjTotal());
        return out;
    }

}
